package com.zprmb.udzt;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.ll.BaseHelper;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.Regular;
import com.zprmb.udzt.util.VolleryUtil;
/**
 * 注册/登录
 * @author shenshao
 *
 */
public class Log_or_RegisActivity extends BaseLogActivity implements OnClickListener{
	private EditText et_user_phone;
	private Button btn_next;
	private String phone;
	private ImageView img_back;
	private int who=0;
	private RelativeLayout rl_back;
	private TextView tv_customer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
     BbtApplication.getInstance().addActvity(this);
		
		et_user_phone=(EditText)findViewById(R.id.et_user_phone);
		btn_next=(Button)findViewById(R.id.next_btn);
		img_back=(ImageView)findViewById(R.id.img_back);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		tv_customer=(TextView)findViewById(R.id.enterprise_tel);
		tv_customer.setOnClickListener(this);
		rl_back.setOnClickListener(this);
		img_back.setOnClickListener(this);
		who=getIntent().getIntExtra("who", 0);
	    Drawable drawable = new BitmapDrawable();
	     et_user_phone.setCompoundDrawables(drawable, null, null, null);
	    drawable = this.getResources().getDrawable(R.drawable.zch1);
	    et_user_phone.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
	    btn_next.setOnClickListener(this);
//	    et_user_phone.setText(cur_url);
	    et_user_phone.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@SuppressLint("NewApi")
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if(et_user_phone.getText().toString().length()<11){
					btn_next.setBackgroundResource(R.drawable.btn_unclick);
					btn_next.setEnabled(false);
				}else{
					btn_next.setBackgroundResource(R.drawable.my_button);
					btn_next.setEnabled(true);
				}
			}
		});
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_BACK) {
		if (who==3) {
			//return false;
			finish();
		}else {
			Intent intent = new Intent(this,MainActivity.class);
			startActivity(intent);
			finish();
			return super.onKeyDown(keyCode, event);
		}
	}
	return super.onKeyDown(keyCode, event);
}
	private void nextData() {
		// TODO Auto-generated method stub
		phone=et_user_phone.getText().toString();
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				btn_next.setBackgroundResource(R.drawable.my_button);
				btn_next.setEnabled(true);
				if(jsonObject.optString("data").equals("true")){
					Intent intent1=new Intent(Log_or_RegisActivity.this,LoginActivity.class);
					intent1.putExtra("phone", phone);
					intent1.putExtra("towhere", getIntent().getIntExtra("towhere", 0));
					if(getIntent().getIntExtra("who", 0)==4){
						
						intent1.putExtra("who", 4);
					}else if(getIntent().getIntExtra("who", 0)==3){
						intent1.putExtra("who", 3);
					}else if(getIntent().getIntExtra("who", 0)==5){
						intent1.putExtra("who", 5);
					}else{
						intent1.putExtra("who", 2);
					}
					startActivity(intent1);
					et_user_phone.setText("");
					if(getIntent().getIntExtra("who", 0)==4){
						Log_or_RegisActivity.this.finish();
					}else if(getIntent().getIntExtra("who", 0)==3){
						Log_or_RegisActivity.this.finish();
					}
					
				}else{
					/*Intent intent=new Intent(Log_or_RegisActivity.this,RegisteredActivity.class);
					intent.putExtra("phone", phone);
					
					startActivity(intent);
					et_user_phone.setText("");*/
				}
//				Toast.makeText(LoginActivity.this, jsonObject.optString("data")+"", 2000).show();
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				btn_next.setBackgroundResource(R.drawable.my_button);
				btn_next.setEnabled(true);
			}
		};
		volleryUtil.setShow(true);
		volleryUtil.setRequestMethod(0);
		volleryUtil.execute(Log_or_RegisActivity.this, Constant.URL+"/api/v5/login/mobileCheck", "?mobile="+phone);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.next_btn:
			if(Regular.isMobileNO(et_user_phone.getText().toString())){
				btn_next.setBackgroundResource(R.drawable.btn_unclick);
				btn_next.setEnabled(false);
				nextData();
			}else{
				Toast.makeText(Log_or_RegisActivity.this, "请输入正确的手机号码", 2000).show();
			}
			break;
		case R.id.img_back:
			if (who==3) {
				//return false;
				finish();
			}else {
				Intent intent8 = new Intent(this,MainActivity.class);
				startActivity(intent8);
				finish();
				
			}
			break;
		case R.id.rl_back:
			finish();
			break;

		case R.id.enterprise_tel:
			BaseHelper.showDialog2(this, "拨号", "400-0101-016", 1);
			break;
		}
	}

	

}
